package cn.rongdao.jrkabao.model.main.tab;

import android.text.TextUtils;

import com.mifengkong.frtools.http.request.HttpFunction;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRSharedPreferences;

import java.util.List;

import cn.rongdao.jrkabao.bean.main.tab.TabBean;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.model.api.BaseKaMaoModel;
import io.reactivex.Observable;
import io.reactivex.Observer;

/**
 * Created by jack on 2017/5/22
 */

public class TabModel extends BaseKaMaoModel {


    private StringBuilder sbTabKey = new StringBuilder();
    private StringBuilder sbTabName = new StringBuilder();
    private OnTabRequestListener onTabRequestListener;

    public static TabModel getInstance() {
        return getPresent(TabModel.class);
    }

    private void execute(Observer<List<TabBean>> observer) {
        addParamStrings("list_code", "kamao_tab_bar");
        Observable<List<TabBean>> observable = mServletApi.getTab(mParams).map(new HttpFunction<List<TabBean>>());
        toSubscribe(observable, observer);
    }

    public TabModel setTab() {
        execute(new FRBaseObserver<List<TabBean>>() {
            @Override
            protected void onBaseNext(List<TabBean> data) {
                if (null != data) {
                    resetStringBuffer();
                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).getConfig_en().equals("JiebaViewController")) {
                            FRSharedPreferences.setStringNotClear(SPConstant.ALL_OTHER_LOAN_URL, data.get(i).getUrl());
                            FRSharedPreferences.setStringNotClear(SPConstant.ALL_OTHER_LOAN_CORNER, data.get(i).getCorner());
                        }
                        sbTabKey = sbTabKey.append(data.get(i).getConfig_en()).append(",");
                        sbTabName = sbTabName.append(data.get(i).getConfig_cn()).append(",");
                    }
                } else {
                    addDefaultTab();
                }
                saveTabValue();
                if (null != onTabRequestListener) {
                    onTabRequestListener.success();
                }
            }

            @Override
            protected void onBaseError(Throwable t) {
                addDefaultTab();
                saveTabValue();
                if (null != onTabRequestListener) {
                    onTabRequestListener.failure(t);
                }
            }
        });
        return this;
    }

    private void saveTabValue() {
        FRSharedPreferences.clearSharePreference(SPConstant.TAB);
        FRSharedPreferences.setString(SPConstant.TAB, SPConstant.TAB_KEY, sbTabKey.toString().substring(0, TextUtils.isEmpty(sbTabKey.toString())
                ? 0 : sbTabKey.length() - 1));
        FRSharedPreferences.setString(SPConstant.TAB, SPConstant.TAB_NAME, sbTabName.toString().substring(0, TextUtils.isEmpty(sbTabName.toString())
                ? 0 : sbTabName.length() - 1));
    }

    private void addDefaultTab() {
        resetStringBuffer();
        sbTabKey = sbTabKey.append("CashAdvanceViewController,ProfileViewController,");
        sbTabName = sbTabName.append("首页,我的,");
    }

    private void resetStringBuffer() {
        sbTabName.delete(0, sbTabName.length());
        sbTabKey.delete(0, sbTabKey.length());
    }

    public void setOnTabRequestListener(OnTabRequestListener onTabRequestListener) {
        this.onTabRequestListener = onTabRequestListener;
    }

    public interface OnTabRequestListener {
        void success();

        void failure(Throwable t);
    }
}